package com.devinlee.designpatterns.singleton;

/**
 * 单例模式-懒汉式
 */
public class Singleton2 {

    // 保证instance在所有线程中同步
    private volatile static Singleton2 singleton;

    // private避免类在外部被实例化
    private Singleton2(){

    }

    /**
     * 双重检查锁
     * @return
     */
    public static Singleton2 getInstance(){
        if (singleton == null){
            synchronized (Singleton2.class){
                if (singleton == null){
                    singleton = new Singleton2();
                }
            }
        }
        return singleton;
    }
}